package edu.java.texbooks.scjp.threads.test18;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TestThreadPool {
	private static final int THREADS_NUMBER = 3;

	public static void main(String[] args) {
		System.out
				.println(Thread.currentThread().getName() + " starts work...");

		// declare Runnable-s

		PrintTask task1 = new PrintTask("task 1");
		PrintTask task2 = new PrintTask("task 2");
		PrintTask task3 = new PrintTask("task 3");
		PrintTask task4 = new PrintTask("task 4");

		// declare fixed thread pool using ExecuterService-class and
		// Executors-class
		ExecutorService threadExecutor = Executors
				.newFixedThreadPool(THREADS_NUMBER);

		// create,start and put in runnable state the threads
		threadExecutor.execute(task1);
		threadExecutor.execute(task2);
		threadExecutor.execute(task3);

		// it works perfectly :)
		threadExecutor.execute(task4);

		// shut down worker threads
		threadExecutor.shutdown();

		System.out.println(Thread.currentThread().getName() + " ends work...");
	}
}
